﻿using NDK.Module.BLL;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using NDK.Motion.StationLogic;
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using static NDK.Module.Model.Common;

namespace NDK.Module.StationLogic
{
    [Monitor(Description = "下料双动子工位")]
    public class FlipStationCatch : StationLogic
    {
        #region 通用
        //记录当前步骤，用于步骤变化的比较
        private StationStep _curStep = StationStep.开始准备;
        //对外提供工位的步骤变化事件
        public override event PropertyChangedEventHandler PropertyChanged;
        //对外提供唯一实例
        public static FlipStationCatch Instance = new FlipStationCatch();
        //私有化的构造函数
        private FlipStationCatch () : base()
        {
            this.Name = "下料双动子工位";
        }
        //步骤字段、属性
        private StationStep _step = StationStep.开始准备;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _step;
            }
            set
            {
                if(_curStep != value)
                {
                    _step = value;
                    _curStep = value;

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion


        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            Step = StationStep.开始准备;
            logBaseInfo = "FlipStationCatch";
        }

        private bool VacuumOn ()
        {
            return VacuumMethod(true, (Machine.do_翻转平台真空2吸, Machine.di_翻转平台真空2反馈, true));
        }

        public void VacuumOff ()
        {
            Machine.do_翻转平台真空2吸.OFF();
            Machine.do_翻转平台真空2破.ON();
            Thread.Sleep(150);
            Machine.do_翻转平台真空2破.ON();
        }

        /// <summary>
        /// 工站动作流程
        /// </summary>
        /// <param name="mode"></param>
        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.开始准备:
                        Step = StationStep.流程开始;
                        break;

                    case StationStep.流程开始:
                        Step = StationStep.工站要料;
                        break;

                    case StationStep.工站要料:
                        工位要料 = true;
                        Step = StationStep.等待上工站开完成;
                        break;

                    case StationStep.等待上工站开完成:
                        if(FlipStationPut.Instance.工位完成)
                        {
                            Common.sw8.Restart();
                            FlipStationPut.Instance.VacuumOff();
                            Step = StationStep.打开真空;
                        }
                        break;

                    case StationStep.打开真空:
                        if(VacuumOn())
                        {
                            工位要料 = false;
                            Step = StationStep.克隆数据;
                        }
                        break;

                    case StationStep.克隆数据:
                        Step = StationStep.等待上工站关完成;
                        break;

                    case StationStep.等待上工站关完成:
                        if(!FlipStationPut.Instance.工位完成)
                        {
                            Step = StationStep.等待下工站开要料;
                        }
                        break;

                    case StationStep.等待下工站开要料:
                        if(Transform.Instance.工位要料)
                        {
                            工位完成 = true;
                            Step = StationStep.等待下工站关要料;
                        }
                        break;

                    case StationStep.等待下工站关要料:
                        if(!Transform.Instance.工位要料)
                        {
                            工位完成 = false;
                            Step = StationStep.模式判断;
                        }
                        break;

                    case StationStep.模式判断:
                        if((!Common.GRR循环模式 || Common.GRREnd) && !Common.pointCheck)
                        {
                            Step = StationStep.流程结束;
                        }
                        else
                        {
                            GRR工位要料 = true;
                            Step = StationStep.GRR等待下工站开完成;
                        }
                        break;

                    case StationStep.GRR等待下工站开完成:
                        if(Transform.Instance.GRR工位完成)
                        {
                            Transform.Instance.VacuumOff();
                            Step = StationStep.GRR打开真空吸;
                        }
                        break;

                    case StationStep.GRR打开真空吸:
                        if(VacuumOn())
                        {
                            GRR工位要料 = false;
                            Step = StationStep.GRR等待下工位关完成;
                        }
                        break;

                    case StationStep.GRR等待下工位关完成:
                        if(!Transform.Instance.GRR工位完成)
                        {
                            GRR工位完成 = true;
                            Step = StationStep.GRR等待上工位关要料;
                        }
                        break;

                    case StationStep.GRR等待上工位关要料:
                        if(!FlipStationPut.Instance.GRR工位要料)
                        {
                            GRR工位完成 = false;
                            Step = StationStep.流程结束;
                        }
                        break;

                    case StationStep.流程结束:
                        Common.sw8.Stop();
                        Step = StationStep.开始准备;
                        break;
                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[FlipStationCatch ActionProcess Thread ERROR] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();

        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        /// <summary>
        /// 工位步骤定义
        /// </summary>
        public enum StationStep
        {
            /// <summary>
            /// 等待机台初始化
            /// </summary>
            [Description("等待机台初始化")]
            开始准备,
            [Description("开始")]
            流程开始,
            工站要料,
            等待上工站开完成,
            打开真空,
            克隆数据,
            等待上工站关完成,
            等待下工站开要料,
            等待下工站关要料,
            等待移栽取料到位,
            关闭真空吸,
            通知移栽可离开,
            等待移栽离开,
            模式判断,
            GRR等待下工站开完成,
            GRR打开真空吸,
            GRR等待下工位关完成,
            GRR等待上工位关要料,
            GRR通知翻转可取料,
            GRR等待翻转取料到位,
            GRR关闭真空吸,
            GRR通知翻转可离开,
            GRR等待翻转离开完成,
            [Description("工站完成")]
            流程结束,
        }

    }
}
